/*
 * Project: Daycare Web Application
 * Package: com.madetoordersoftware.daycare.business
 * File:    ParentMgr
 * Created: Jun 3, 2010
 */
/**
 * Copyright (c) 2010 Made 2 Order Software, LLC
 *
 * All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 * transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 * You may not distribute the URL or redistribute the files to third parties. You may transfer the files onto
 * a single storage device so long as you can prove ownership of the software. You may not reverse engineer,
 * decompile, or disassemble the software or media files. You may not modify or translate the software or
 * media, or distribute copies of the software or media without the written consent of Made 2 Order Software,
 * LLC.
 */
package com.made2ordersoftware.daycare.business;

import com.made2ordersoftware.daycare.domain.AddressEntity;
import com.made2ordersoftware.daycare.domain.EmailEntity;
import com.made2ordersoftware.daycare.domain.ParentEntity;
import com.made2ordersoftware.daycare.domain.PhoneEntity;
import com.made2ordersoftware.daycare.domain.enums.AddressType;
import com.made2ordersoftware.daycare.domain.enums.EmailType;
import com.made2ordersoftware.daycare.domain.enums.PhoneType;
import com.made2ordersoftware.daycare.domain.enums.State;
import com.made2ordersoftware.daycare.service.ParentDao;
import com.made2ordersoftware.exceptions.DAOException;
import com.made2ordersoftware.exceptions.InvalidFormatException;
import com.made2ordersoftware.util.StringUtility;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class used to manage all actions performed with a Parent object.
 *
 * @author David Canning <dcannin@gmail.com>
 * @version 1.0.0
 */
@Stateless
public class ParentMgr {

    private static final Logger LOG = LoggerFactory.getLogger(ParentMgr.class);
    @EJB private ParentDao parentDao;

    /**
     * Add a new parent to the day care system.
     *
     * @param firstName First name of parent.
     * @param lastName  Last name of parent.
     * @param street    Street part of address of parent.
     * @param city      City part of address of parent.
     * @param state     State part of address of parent.
     * @param zip       Zip (postal) part of address of parent.
     * @param workPhone Work number of parent.
     * @param homePhone Home number of parent.
     * @param cellPhone Cell number of parent.
     * @param email     Email address of parent.
     *
     * @throws DAOException           Any exception thrown by the DAO layer, usually a database exception.
     * @throws InvalidFormatException Thrown if the String argument is not formatted correctly.
     */
    public void addParent(String firstName, String lastName, String street, String city, State state,
            String zip, String workPhone, String homePhone, String cellPhone, String email) throws
            DAOException, InvalidFormatException {
        LOG.info("Adding parent");

        ParentEntity parent = new ParentEntity(firstName, lastName);
        parentDao.create(parent);

        List<PhoneEntity> phoneNumbers = new ArrayList<>(3);
        if (workPhone != null && !workPhone.isEmpty()) {
            PhoneEntity p1 = new PhoneEntity(StringUtility.validatePhoneNumber(workPhone), PhoneType.WORK);
            p1.setParent(parent);
            phoneNumbers.add(p1);
        }
        if (homePhone != null && !homePhone.isEmpty()) {
            PhoneEntity p2 = new PhoneEntity(StringUtility.validatePhoneNumber(homePhone), PhoneType.HOME);
            p2.setParent(parent);
            phoneNumbers.add(p2);
        }
        if (cellPhone != null && !cellPhone.isEmpty()) {
            PhoneEntity p3 = new PhoneEntity(StringUtility.validatePhoneNumber(cellPhone), PhoneType.CELL);
            p3.setParent(parent);
            phoneNumbers.add(p3);
        }
        parent.setPhoneNumbers(phoneNumbers);

        StringUtility.validateZipCode(zip);
        AddressEntity a1 = new AddressEntity(street, city, state, zip, AddressType.HOME);
        a1.setParent(parent);
        List<AddressEntity> addresses = new ArrayList<>(2);
        addresses.add(a1);
        parent.setAddresses(addresses);

        List<EmailEntity> emails = new ArrayList<>(2);
        if (email != null && !email.isEmpty()) {
            EmailEntity e1 = new EmailEntity(email, EmailType.HOME);
            e1.setParent(parent);
            emails.add(e1);
        }
        parent.setEmails(emails);

        parentDao.update(parent);
        LOG.info("Person added successfully");
    }

}
